package com.lasun.server.entity;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.HashMap;

public class World {

	// Hashtable节奏
	// private Hashtable<Integer,Client> clients = new
	// Hashtable<Integer,Client>();

	private Map<Integer, Client> clientMap = new ConcurrentHashMap<Integer, Client>();

	private static final World world = new World();

	private int index = 1;

	public static World getWorld() {
		return world;
	}

	public void register(Client client) {
		// client.getChannel();
		client.setId(index);
		clientMap.put(client.getChannel().hashCode(), client);
		// if(index%1000 == 0){
		System.out.println("客户端数量" + clientMap.size());
		for (Map.Entry<Integer, Client> entry : clientMap.entrySet()) {
			System.out.println("key= " + entry.getKey() + " and value= "
					+ entry.getValue().getChannel().isActive());
		}
		// }
		index++;
	}

	public void unregister(Integer id) {
		if (clientMap.containsKey(id)) {
			clientMap.remove(id);
			System.out.println("客户端数量" + clientMap.size());
		}
	}

	public Map<Integer, Client> getClients() {
		return clientMap;
	}

}